package com.werdior.shiro.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.werdior.shiro.entity.UserRole;
import com.werdior.shiro.mapper.UserRoleMapper;
import com.werdior.shiro.service.IUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * user_role 用户角色关联表 服务实现类
 * </p>
 *
 * @author werdior
 * @since 2017-12-23
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public void saveOrUpdate(Long id, List<Long> roleIdList) {
        if(roleIdList.size() == 0){
            return ;
        }
        //先删除用户与角色关系
        EntityWrapper<UserRole> ew=new EntityWrapper<>();
        ew.eq("uid",id);
        super.delete(ew);
        roleIdList.forEach(k->{
            UserRole sysUserRole=new UserRole();
            sysUserRole.setUid(id);
            sysUserRole.setRid(k.longValue());
            super.insert(sysUserRole);
        });
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        List<Long> longList=new ArrayList<>();
        EntityWrapper<UserRole> ew=new EntityWrapper<>();
        ew.eq("uid",userId);
        List<UserRole> sysUserRoles=selectList(ew);
        sysUserRoles.forEach(k->{
            longList.add(k.getRid());
        });
        return longList;
    }
}
